package com.nic.view.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.nic.view.R;
import com.nic.view.util.glide.GlideUtil;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 图片采集
 * Created by Administrator on 2018/11/8.
 */

public class TakePhotoAdapter extends BaseAdapter {

    private LinkedList<String> imagePathList = new LinkedList<>();
    private Context context;
    private boolean isAddMultiPhoto = true;
    private boolean isPreview;

    /**
     * 是否预览
     *
     * @param preview
     */
    public void setPreview(boolean preview) {
        isPreview = preview;
    }

    public List<String> getList() {
        List<String> list = new ArrayList<>();
        for (String s : imagePathList) {
            if (!TextUtils.isEmpty(s)) {
                list.add(s);
            }
        }
        return list;
    }

    /**
     * 是否添加多张图片
     *
     * @param isAddMultiPhoto
     */
    public void setIsAddMultiPhoto(boolean isAddMultiPhoto) {
        this.isAddMultiPhoto = isAddMultiPhoto;
    }

    public TakePhotoAdapter(Context context) {
        this.context = context;
    }

    public void setList(List<String> pathList) {
        this.imagePathList.clear();
        if (pathList != null && !pathList.isEmpty()) {
            this.imagePathList.addAll(pathList);
        }
        if ((!isPreview && isAddMultiPhoto) || (!isPreview && this.imagePathList.isEmpty())) {//非预览
            imagePathList.addLast("");
        }
        notifyDataSetChanged();
    }

    public void addLast(String path) {
        if (TextUtils.isEmpty(path)) {
            return;
        }

        if (!isAddMultiPhoto || TextUtils.isEmpty(imagePathList.getLast())) {
            imagePathList.removeLast();
        }
        this.imagePathList.addLast(path);
        if ((!isPreview && this.imagePathList.isEmpty()) || (!isPreview && isAddMultiPhoto)) {
            imagePathList.addLast("");
        }
        notifyDataSetChanged();
    }

    public void remove(int position) {
        if (position >= 0 && position < imagePathList.size()) {
            if (imagePathList.size() == 1) {
                imagePathList.clear();
                imagePathList.add("");
            } else {
                imagePathList.remove(position);
            }
            notifyDataSetChanged();
        }
    }


    @Override
    public int getCount() {
        return imagePathList.size();
    }

    @Override
    public Object getItem(int i) {
        return imagePathList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_choose_pic, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_add = (ImageView) convertView.findViewById(R.id.iv_add_photo);
            viewHolder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete_photo);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        String path = (String) getItem(position);
//        viewHolder.iv_add.setTag(R.id.glide_image_id, path);
//        String tag = (String) viewHolder.iv_add.getTag(R.id.glide_image_id);
//         && tag == path
        if (!TextUtils.isEmpty(path)) {
            GlideUtil.getInstance().load(path, viewHolder.iv_add);
            if (!isPreview) {
                viewHolder.iv_delete.setVisibility(View.VISIBLE);
                viewHolder.iv_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        remove(position);
                    }
                });
            } else {
                viewHolder.iv_delete.setVisibility(View.GONE);
            }
        } else {
            GlideUtil.getInstance().clear(viewHolder.iv_add);
            viewHolder.iv_add.setImageResource(R.mipmap.ic_add_pic);
            viewHolder.iv_delete.setVisibility(View.GONE);
        }

        return convertView;
    }

//    private void jumpPreview(Context context) {
//        if (imagePathList == null || imagePathList.isEmpty()) {
//            T.show(context, "暂无可预览图片", Toast.LENGTH_SHORT);
//            return;
//        }
//        Intent intent = new Intent(context, PhotoPreviewActivity.class);
//        intent.putExtra(PhotoPreviewActivity.LIST, (Serializable) imagePathList);
//        context.startActivity(intent);
//    }

    class ViewHolder {
        ImageView iv_add;
        ImageView iv_delete;
    }
}
